﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

using NationalInstruments.VisaNS;

namespace chromemeter
{
    class ClassSourceMeter
    {
         private MessageBasedSession mbSession;
        private const string resourceName = "USB0::0x05E6::0x2450::04380225::INSTR";
        public int Join()
        {
            int ret = 0; //-------------------------------连接成功标志位

          

           
                try
                {
                    mbSession = (MessageBasedSession)ResourceManager.GetLocalManager().Open(resourceName);
                }
                catch
                {
                    ret = 1;
                }
             

            
                

            return ret;
            }

        


        
       /* public void VoltSweep(double start, double stop, double step, double delay, int count,int num, double ilim)
        {
           

                mbSession.Write("*RST");
                
                //mbSession.Write("TRACE:MAKE 'defbuffer1', 500");
                mbSession.Write("TRACe:CLEar 'defbuffer1'");
                mbSession.Write("SOUR:FUNC VOLT");
                mbSession.Write("SOUR:VOLT:RANGE:AUTO ON");
                mbSession.Write("SENS:FUNC 'CURR'");
                mbSession.Write("SENS:CURR:RANGE:AUTO ON");
                mbSession.Write("SOUR:VOLT:ILIM " + ilim.ToString("#0.0000"));
                mbSession.Write("SOUR:SWE:VOLT:LIN:STEP "+ start.ToString("#0.0000")+", " + stop.ToString("#0.0000")+", " +  step.ToString("#0.0000")+", " + delay.ToString("#0.0000")+", " + count.ToString("#0.0000")+", " + "FIXED, ON, OFF, 'defbuffer1'");
                mbSession.Write("INIT");
                mbSession.Write("*WAI");
                mbSession.Write("TRAC:DATA? 1"+", "+ num.ToString("#0.0000")+","+" 'defbuffer1', SOUR, READ");
                mbSession.Write("TRACe:CLEar 'defbuffer1'");
                
            
        }

       
        public void CurrSweep(double start, double stop, double step, double delay, int count, int num,double vlim)
        {


            mbSession.Write("*RST");
           // mbSession.Write("TRACe:CLEar 'Mybuffer'");
            mbSession.Write("TRACE:MAKE 'Mybuffer', 500");
            mbSession.Write("TRACe:CLEar 'Mybuffer'");
            mbSession.Write("SOUR:FUNC CURR");
            mbSession.Write("SOUR:CURR:RANGE:AUTO ON");
            mbSession.Write("SENS:FUNC 'VOLT'");
            mbSession.Write("SENS:VOLT:RANGE:AUTO ON");
            mbSession.Write("SOUR:CURR:VLIM " + vlim.ToString("#0.00"));
            mbSession.Write("SOUR:SWE:CURR:LIN:STEP " + start.ToString("#0.0000") + ", " + stop.ToString("#0.0000") + ", " + step.ToString("#0.0000") + ", " + delay.ToString("#0.0000") + ", " + count.ToString("#0.0000") + ", " + "FIXED, ON, OFF, 'Mybuffer'");
            mbSession.Write("INIT");
            mbSession.Write("*WAI");
            mbSession.Write("TRAC:DATA? 1" + ", " + num.ToString("#0.0000") + "," + " 'Mybuffer', SOUR, READ");
            mbSession.Write("TRACe:CLEar 'Mybuffer'");

            
        }*/

        public void InitCurr( double vlim)
        {


            mbSession.Write("*RST");
            mbSession.Write("TRACe:CLEar 'defbuffer1'");
            mbSession.Write("SOUR:FUNC CURR");
            mbSession.Write("SOUR:CURR:RANGE:AUTO ON");
            mbSession.Write("SENS:FUNC 'VOLT'");
            mbSession.Write("SENS:VOLT:RANGE:AUTO ON");
            mbSession.Write("SOUR:CURR:VLIM " + vlim.ToString("#0.00"));
            mbSession.Write("SOUR:CURR:READ:BACK ON");
            //mbSession.Write("SOUR:CURR "+ cur.ToString("#0.000000"));
            //mbSession.Write(":OUTP ON");

        }
        public void InitVolt(double ilim)
        {


            mbSession.Write("*RST");
            mbSession.Write("TRACe:CLEar 'defbuffer1'");
            mbSession.Write("SOUR:FUNC VOLT");
            mbSession.Write("SOUR:VOLT:RANGE:AUTO ON");
            mbSession.Write("SENS:FUNC 'CURR'");
            mbSession.Write("SENS:CURR:RANGE:AUTO ON");
            mbSession.Write("SOUR:VOLT:ILIM " + ilim.ToString("#0.00"));
            mbSession.Write("SOUR:VOLT:READ:BACK ON");
            //mbSession.Write("SOUR:CURR "+ cur.ToString("#0.000000"));
            //mbSession.Write(":OUTP ON");

        }

        public void SourCurr(double curr)
        {
            mbSession.Write("SOUR:CURR " + curr.ToString("#0.000000"));
            mbSession.Write(":OUTP ON");
            mbSession.Write("TRACe:TRIGger 'defbuffer1'");
        }
        public void SourVolt(double volt)
        {
            mbSession.Write("SOUR:VOLT " + volt.ToString("#0.000000"));
            mbSession.Write(":OUTP ON");
            mbSession.Write("TRACe:TRIGger 'defbuffer1'");
        }

        public void Receive()
        {
            mbSession.Write("TRACe:TRIGger 'defbuffer1'");
            mbSession.Write("TRAC:DATA? 1, 1, 'defbuffer1', SOUR, READ");
        }

       /* public void VoltMeasure( ref double vol, ref double cur)
        {
            string recv;
            
            mbSession.Write("TRAC:DATA? 1, 1, 'defbuffer1', SOUR, READ");
            mbSession.Timeout = 120000;
            recv = mbSession.ReadString(4096);
            string str = recv;
            string[] strArr = str.Split(',');
            double[] currArr = new double[1];
            double[] voltArr = new double[1];
            if (strArr != null && strArr.Length > 0)
            {
                double.TryParse(strArr[0], out voltArr[0]);
                double.TryParse(strArr[1], out currArr[0]);

            }
            vol = voltArr[0];
            cur = currArr[0];


        }*/

        public void VoltMeasure(ref int num, ref double[] vol, ref double[] cur)
        {
            
                    string recv;
                    mbSession.Write("TRAC:DATA? 1" + ", " + num.ToString("#0.0000") + "," + " 'defbuffer1', SOUR, READ");
                    mbSession.Timeout = 120000;
                    recv = mbSession.ReadString(4096);
                    string str = recv;
                    string[] strArr = str.Split(',');
                    double[] currArr = new double[strArr.Length / 2];
                    double[] voltArr = new double[strArr.Length / 2];
                    if (strArr != null && strArr.Length > 0)
                    {
                        for (int i = 0, a = 0; i < strArr.Length; i = i + 2, a++)
                        {
                            double.TryParse(strArr[i], out currArr[a]);
                        }
                        for (int j = 1, b = 0; j < strArr.Length; j = j + 2, b++)
                        {
                            double.TryParse(strArr[j], out voltArr[b]);
                        }

                    }
                    vol = voltArr;
                    cur = currArr;
                    
                
            }
      /*  public void CurrMeasure( ref string recv, ref double[] vol, ref double[] cur)
        {
           

                mbSession.Timeout = 120000;
                recv = mbSession.ReadString(4096);
                string str = recv;
                string[] strArr = str.Split(',');
                double[] currArr = new double[strArr.Length / 2];
                double[] voltArr = new double[strArr.Length / 2];
                if (strArr != null && strArr.Length > 0)
                {
                    for (int i = 0, a = 0; i < strArr.Length; i = i + 2, a++)
                    {
                        double.TryParse(strArr[i], out currArr[a]);
                    }
                    for (int j = 1, b = 0; j < strArr.Length; j = j + 2, b++)
                    {
                        double.TryParse(strArr[j], out voltArr[b]);
                    }

                }
                vol = voltArr;
                cur = currArr;

            
        }*/
        public void CurrMeasure(ref int num, ref double[] vol, ref double[] cur)
        {

            string recv;
            mbSession.Write("TRAC:DATA? 1" + ", " + num.ToString("#0.0000") + "," + " 'defbuffer1', SOUR, READ");
            mbSession.Timeout = 120000;
            recv = mbSession.ReadString(4096);
            string str = recv;
            string[] strArr = str.Split(',');
            double[] currArr = new double[strArr.Length / 2];
            double[] voltArr = new double[strArr.Length / 2];
            if (strArr != null && strArr.Length > 0)
            {
                for (int i = 0, a = 0; i < strArr.Length; i = i + 2, a++)
                {
                    double.TryParse(strArr[i], out voltArr[a]);
                }
                for (int j = 1, b = 0; j < strArr.Length; j = j + 2, b++)
                {
                    double.TryParse(strArr[j], out currArr[b]);
                }

            }
            vol = voltArr;
            cur = currArr;


        }
       /* public void CurrMeasure(ref string recv, ref double vol, ref double cur)
        {

            mbSession.Write("TRACe:TRIGger 'defbuffer1'");
            mbSession.Write("TRAC:DATA? 1, 1, 'defbuffer1', SOUR, READ");
            mbSession.Timeout = 120000;
            recv = mbSession.ReadString(4096);
            string str = recv;
            string[] strArr = str.Split(',');
            double[] currArr = new double[1];
            double[] voltArr = new double[1];
            if (strArr != null && strArr.Length > 0)
            {
                double.TryParse(strArr[0], out currArr[0]);
                double.TryParse(strArr[0], out voltArr[0]);
            }
            vol = voltArr[0];
            cur = currArr[0];


        }*/
        public void LightOn()
        {
           
           

                mbSession.Write(":OUTP ON");
                
            
        }

        public void LightOff()
        {


            mbSession.Write(":OUTP OFF");
            
        }

        public void Eixt()
        {
            
            

                mbSession.Dispose();
                
            
        }
    }
}
